/**
* Created by wl
* 项目管理>持有中-添加页
*/
<template>
    <div>
        <div class="right right__detail">
            <div class="ca-align-right">
                <div class="ca-head-fixed">
                    <el-button type="primary" @click="saveInfo">保存</el-button>
                    <el-button @click="exit">退出</el-button>
                </div>
            </div>
            <csHeader @fundEntity="getFundEntity" @fundUserList="getFundUserList" @fundMotherSonList="getFundMotherSonList" @industryList="getIndustryList" :fundState="state" ref="headerInfo"></csHeader>
            <csFundInfo @fundReserveInfo="getFundReserveInfo"></csFundInfo>
            <csManagementAgency @fundMechanism="getFundMechanism" ref="managementInfo"></csManagementAgency>
        </div>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from './api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind } from '__service/utils';
    export default {
        data() {
            return {
                itemId: '', //id
                fundEntity: {}, //头部基本信息
                fundMechanism: {}, //管理机构
                fundMotherSonList: [], //母基金
                fundReserve: {}, //基金信息
                fundUserList: [], //基金经理和基金团队
                industryList: [], //投资行业
            }
        },
        watch: {
        },
        props: {
            state: {
                type: Number,
                default: function () {
                    return 6;
                }
            },
        },
        mounted() {
        },
        methods: {
            getFundEntity(fundEntity) {
                this.fundEntity = fundEntity
            },
            getFundUserList(fundUserList) {
                this.fundUserList = fundUserList
            },
            getFundMotherSonList(fundMotherSonList) {
                this.fundMotherSonList = fundMotherSonList
            },
            getFundReserveInfo(fundReserveInfo) {
                this.fundReserve = fundReserveInfo
            },
            getFundMechanism(fundMechanism) {
                this.fundMechanism = fundMechanism
            },
            getIndustryList(industryList) {
                this.industryList = industryList
            },
            // 退出
            exit() {
                this.$confirm('是否退出', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                    if (this.state === 0) {
                        this.$router.push({name: 'pre-investment-funds-list'})
                    } else {
                        this.$router.push({name: 'fund-reserve'})
                    }
                })
            },
            // 保存
            saveInfo() {
                let isSave = this.$refs.headerInfo.validateAdd();  // 验证通过，可以保存
                let isSaveManagement = this.$refs.managementInfo.validateAddInfo();  // 验证通过，可以保存
                let managerType = []
                this.fundUserList.map(item => {
                    managerType.push(Number(item.userType))
                })
                if (managerType.indexOf(1) < 0) {
                    this.$message.error('请选择基金经理')
                    return false
                }
                if (managerType.indexOf(2) < 0) {
                    this.$message.error('请选择项目组成员')
                    return false
                }
                if (this.FOF) {
                    if (this.fundMotherSonList.length <= 0) {
                        this.$message.error('请选择母基金')
                        return false
                    }
                }
                if (isSave && isSaveManagement) {
                    let query = {
                        fundEntity: this.fundEntity, //头部基本信息
                        fundMechanism: this.fundMechanism, //管理机构
                        fundMotherSonList: this.fundMotherSonList, //母基金
                        fundReserve: this.fundReserve, //基金信息
                        fundUserList: this.fundUserList, //基金经理和基金团队
                        industryList: this.industryList, //投资行业
                    }
                    apiHttp(api.ADD_FUNDRESERV, query).then(ret=> {
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                message: '数据添加成功！',
                                type: 'success',
                                center: true
                            });
                            this.itemId = ret.data.id
                            this.save()
                            if (this.state === 0) {
                                this.$router.push({name: 'pre-investment-funds-list'})
                            } else {
                                this.$router.push({name: 'fund-reserve'})
                            }
                        }
                    })
                }
            },
            async save() {
                // 添加小红点
                await addRemind({
                    dataId: this.itemId, // 业务id
                    dataTab: 0, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 4, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
        },
        components: {
            csHeader: require('./header/edit'), //头部信息
            csFundInfo: require('./tab/fund-info/edit'), //基金信息
            csManagementAgency: require('./tab/management-agency/edit'), //管理机构信息
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>